import ticaiDataBase from "@/static/ticaiData.json";
import fucaiDataBase from "@/static/fucaiData.json";

const ticaiTableData = ticaiDataBase.map((item) => {
  const result =
    item.result < 10
      ? `00${item.result}`
      : item.result < 100
      ? `0${item.result}`
      : `${item.result}`;
  return {
    ...item,
    result,
    date: item.date.substring(2).replace(/-/g, "/"),
    ...getExtraValue(Array.from(result).map((i) => +i)),
  };
});

const fucaiTableData = fucaiDataBase.map((item) => {
  const result =
    item.result < 10
      ? `00${item.result}`
      : item.result < 100
      ? `0${item.result}`
      : `${item.result}`;
  return {
    ...item,
    result,
    date: item.date.substring(2).replace(/-/g, "/"),
    ...getExtraValue(Array.from(result).map((i) => +i)),
  };
});

export function useDatabase(type) {
  const data = type === "ticai" ? ticaiTableData : fucaiTableData;
  return {
    tableData: type === "ticai" ? ticaiTableData : fucaiTableData,
    getTrend: (len) => getTrendStatic(len, data),
  };
}

// 大小、奇偶形态分布
function getTrendStatic(len = 200, tableData) {
  const data = tableData.slice(0, len);
  const sizeTrendPayload = {
    xxx: { interval: [], trend: [] },
    ddd: { interval: [], trend: [] },
    xxd: { interval: [], trend: [] },
    xdx: { interval: [], trend: [] },
    dxx: { interval: [], trend: [] },
    xdd: { interval: [], trend: [] },
    dxd: { interval: [], trend: [] },
    ddx: { interval: [], trend: [] },
  };
  const parityTrendPayload = {
    jjj: { interval: [], trend: [] },
    ooo: { interval: [], trend: [] },
    joo: { interval: [], trend: [] },
    ojo: { interval: [], trend: [] },
    ooj: { interval: [], trend: [] },
    jjo: { interval: [], trend: [] },
    joj: { interval: [], trend: [] },
    ojj: { interval: [], trend: [] },
  };
  const zhiheTrendPayload = {
    zzz: { interval: [], trend: [] },
    hhh: { interval: [], trend: [] },
    zzh: { interval: [], trend: [] },
    zhz: { interval: [], trend: [] },
    hzz: { interval: [], trend: [] },
    zhh: { interval: [], trend: [] },
    hzh: { interval: [], trend: [] },
    hhz: { interval: [], trend: [] },
  };

  const sizeFormMap = {
    xxx: "小小小",
    ddd: "大大大",
    xxd: "小小大",
    xdx: "小大小",
    dxx: "大小小",
    xdd: "小大大",
    dxd: "大小大",
    ddx: "大大小",
  };

  const parityFormMap = {
    jjj: "奇奇奇",
    ooo: "偶偶偶",
    joo: "奇偶偶",
    ojo: "偶奇偶",
    ooj: "偶偶奇",
    jjo: "奇奇偶",
    joj: "奇偶奇",
    ojj: "偶奇奇",
  };

  const zhiheFormMap = {
    zzz: "质质质",
    hhh: "合合合",
    zzh: "质质合",
    zhz: "质合质",
    hzz: "合质质",
    zhh: "质合合",
    hzh: "合质合",
    hhz: "合合质",
  };

  const renderMap = [
    {
      formMap: sizeFormMap,
      trendPayload: sizeTrendPayload,
      formKey: "sizeForm",
    },
    {
      formMap: parityFormMap,
      trendPayload: parityTrendPayload,
      formKey: "parityForm",
    },
    {
      formMap: zhiheFormMap,
      trendPayload: zhiheTrendPayload,
      formKey: "zhiheForm",
    },
  ];

  function renderPayload(item, idx) {
    renderMap.forEach((sub) => {
      const { formMap, trendPayload, formKey } = sub;

      Object.keys(formMap).forEach((key) => {
        if (item[formKey] === formMap[key]) {
          // 将出现的序列插入到数组中
          trendPayload[key].interval.push(idx);

          // 计算出间隔
          if (!trendPayload[key].trend.length) {
            trendPayload[key].trend.push(idx);
          } else {
            const len = trendPayload[key].interval.length;
            trendPayload[key].trend.push(
              idx - trendPayload[key].interval[len - 2] - 1
            );
          }
        }
      });
    });
  }

  data.forEach(renderPayload);

  const sizeTrendData = Object.keys(sizeTrendPayload).map((key) => {
    const dataItem = {};
    dataItem.sizeForm = sizeFormMap[key];
    dataItem.interval = sizeTrendPayload[key].interval;
    dataItem.trend = sizeTrendPayload[key].trend;
    return dataItem;
  });

  const parityTrendData = Object.keys(parityTrendPayload).map((key) => {
    const dataItem = {};
    dataItem.parityForm = parityFormMap[key];
    dataItem.interval = parityTrendPayload[key].interval;
    dataItem.trend = parityTrendPayload[key].trend;
    return dataItem;
  });

  const zhiheTrendData = Object.keys(zhiheTrendPayload).map((key) => {
    const dataItem = {};
    dataItem.zhiheForm = zhiheFormMap[key];
    dataItem.interval = zhiheTrendPayload[key].interval;
    dataItem.trend = zhiheTrendPayload[key].trend;
    return dataItem;
  });
  return { sizeTrendData, parityTrendData, zhiheTrendData };
}

// 基础信息
function getExtraValue(arr) {
  const max = Math.max(...arr);
  const min = Math.min(...arr);
  let sumValue = 0;
  let big = 0; // 大
  let small = 0; // 小
  let sizeForm = "";
  let odd = 0; // 奇
  let even = 0; // 偶
  let parityForm = "";
  let zhi = 0; // 质
  let he = 0; // 合
  let zhiheForm = "";

  arr.forEach((item) => {
    if (item > 4) {
      big++;
      sizeForm += "大";
    } else {
      small++;
      sizeForm += "小";
    }

    if (item % 2 === 0) {
      even++;
      parityForm += "偶";
    } else {
      odd++;
      parityForm += "奇";
    }

    if ([1, 2, 3, 5, 7].includes(+item)) {
      zhi++;
      zhiheForm += "质";
    } else {
      he++;
      zhiheForm += "合";
    }

    sumValue += item;
  });
  return {
    sumValue,
    span: max - min,
    sizeForm,
    sizeRatio: `${big}:${small}`,
    parityForm,
    parityRatio: `${odd}:${even}`,
    zhiheForm,
    zhiheRatio: `${zhi}:${he}`,
  };
}
